package com.admin.education.dataTransferObject.schools.mapper;

import com.admin.education.dataTransferObject.classes.mapper.ClassesMapper;
import com.admin.education.dataTransferObject.grades.Mapper.GradesMapper;
import com.admin.education.dataTransferObject.schools.SchoolsTransfer;
import com.admin.education.dataTransferObject.schools.request.CreateSchoolRequest;
import com.admin.education.dataTransferObject.schools.request.UpdateSchoolRequest;
import com.admin.education.models.Schools;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@Mapper
public abstract class SchoolsMapper {
    @Autowired
    private ClassesMapper classesMapper;
    @Autowired
    private GradesMapper gradesMapper;
    @Mappings({
            @Mapping(target = "classes", ignore = true)
    })
    public abstract SchoolsTransfer mapFrom(Schools schools);

    @Mappings({
    })
    public abstract List<SchoolsTransfer> mapFrom(List<Schools> schools);

    @Mappings({})
    public abstract Schools mapFrom(CreateSchoolRequest createSchoolRequest);

    @Mappings({})
    public abstract Schools mapFrom(UpdateSchoolRequest updateSchoolRequest, @MappingTarget Schools schools);

    @AfterMapping
    public void afterMapping(Schools schools, @MappingTarget SchoolsTransfer schoolsTransfer) {
        if (null != schools.getClasses()) {
            schoolsTransfer.setClasses(classesMapper.mapFrom(schools.getClasses().stream().filter(p -> p.isValid()).collect(Collectors.toList())));
        }
    }
}
